//
//  ViewController.swift
//  JKGPUImageStillCamera
//
//  Created by 王冲 on 2020/8/4.
//  Copyright © 2020 王冲. All rights reserved.
//

import UIKit
import GPUImage
class ViewController: UIViewController {

    @IBOutlet weak var pictureImageView: UIImageView!
    /// 美颜相机对象
    lazy var camera: GPUImageStillCamera = GPUImageStillCamera(sessionPreset: AVCaptureSession.Preset.high.rawValue, cameraPosition: .front)
    /// 滤镜
    lazy var filter: GPUImageBrightnessFilter = GPUImageBrightnessFilter()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // 1.创建 GPUImageStillCamera 相机
        camera.outputImageOrientation = .portrait
        // 1.1、设置滤镜方向(竖屏)
        // 2.创建滤镜(美白/曝光)
        filter.brightness = 0.3
        camera.addTarget(filter)
        
        // 3.创建显示实时画面的View
        let showView = GPUImageView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
        view.insertSubview(showView, at: 0)
        filter.addTarget(showView)
        
        // 4.开始捕捉画面
        camera.startCapture()
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
    }

    @IBAction func takePicturesClick(_ sender: Any) {
   
        camera.capturePhotoAsImageProcessedUp(toFilter: filter) { (image, error) in
            UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)
            self.pictureImageView.image = image
            // 停止画面的捕捉
            self.camera.stopCapture()
        }
    
    }
    
}

